package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    //租约信息
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    //租期信息
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    //支付方式
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    //房间信息
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    //公寓信息
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public AgreementVo customDetail(Long id) {
        //租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        //租期信息
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());
        //支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());
        //房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());
        //公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());

        //装配
        AgreementVo agreementVo = new AgreementVo();
        BeanUtils.copyProperties(leaseAgreement, agreementVo);
        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setLeaseTerm(leaseTerm);
        agreementVo.setPaymentType(paymentType);
        agreementVo.setRoomInfo(roomInfo);
        return agreementVo;

    }

    @Override
    public void customPage(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
        leaseAgreementMapper.customSelectPage(page, queryVo);
    }

    @Override
    public void removeOutStatusById(Long id) {
        LambdaUpdateWrapper<LeaseAgreement> agreementLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        agreementLambdaUpdateWrapper.eq(LeaseAgreement::getId, id);
        agreementLambdaUpdateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING, LeaseStatus.RENEWING);
        LeaseAgreement leaseAgreement = getOne(agreementLambdaUpdateWrapper);
        if (leaseAgreement != null) {
            throw new LeaseException(ResultCodeEnum.ILLEGAL_REQUEST.getMessage(),
                    ResultCodeEnum.SERVICE_ERROR.getCode());
        }
        removeById(id);
    }
}




